package com.ls.config.ai;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        // 创建支持octet-stream的Jackson转换器
        MappingJackson2HttpMessageConverter octetStreamConverter = new MappingJackson2HttpMessageConverter();
        octetStreamConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.APPLICATION_OCTET_STREAM,
                MediaType.APPLICATION_JSON
        ));

        // 添加原始文本转换器用于兜底
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
        stringConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.TEXT_PLAIN,
                MediaType.APPLICATION_OCTET_STREAM
        ));

        // 按优先级添加转换器
        restTemplate.setMessageConverters(Arrays.asList(
                octetStreamConverter,
                new MappingJackson2HttpMessageConverter(),
                stringConverter
        ));

        return restTemplate;
    }
}
